﻿using System.Net.Http;
using System.Web.Http.Filters;
using TY.UC.Log4;

namespace TY.UI.WebApi
{
    /// <summary>
    /// 异常拦截器
    /// </summary>
    public class WebApiExceptionAttribute : ExceptionFilterAttribute
    {
        public override void OnException(HttpActionExecutedContext context)
        {
            Log4NetHelper.WriteLog("WebApiExceptionFilter", context.Exception);

            var message = context.Exception.Message;
            if (context.Exception.InnerException != null)
                message = context.Exception.InnerException.Message;

            context.Response = new HttpResponseMessage() { Content = new StringContent(message) };

            base.OnException(context);
        }
    }
}